package jjn.round2;

/**
 * @author Jiang Jining
 * @since 2023-06-03 20:56
 */
public class LeetCode1156_MaxRepOpt {
    public int maxRepOpt1(String text) {
        int[] cntTotal = new int[26];
        for (char ch : text.toCharArray()) {
            cntTotal[ch - 'a'] += 1;
        }
        int[] cnt = new int[26];
        int ans = 0, maxCount = 0, left = 0;
        for (int i = 0; i < text.length(); i++) {
            int idx = text.charAt(i) - 'a';
            cnt[idx]++;
            if (cntTotal[idx] - 1 > maxCount) {
                maxCount = Math.max(maxCount, cnt[idx]);
            }
            while (i - left + 1 > maxCount + 1) {
                cnt[text.charAt(left) - 'a'] -= 1;
                left += 1;
            }
            ans = Math.max(ans, i - left + 1);
        }
        
        return ans;
    }
}
